package com.example.smartcitydemo.viewModel.smartBus

import androidx.lifecycle.*
import com.example.smartcity_jscs.network.ResultStates.Resource
import com.example.smartcitydemo.bean.BusOrderResponseBean
import com.example.smartcitydemo.bean.SmartBusRequestBean
import com.example.smartcitydemo.ext.getLiveData
import com.example.smartcitydemo.repo.bus.BusRepo
import com.example.smartcitydemo.ui.smartbus.bean.SmartBusExpanableData
import kotlinx.coroutines.Dispatchers

/**
 * @PACKAGE_NAME: com.example.smartcitydemo.viewModel.smartBus
 * @NAME: Caleb.F
 * @DATE: 2021/11/18 21:48
 * @Email: 1919796283@qq.com
 */
class SmartBusViewModel : ViewModel() {


    private val mutableBusData = MutableLiveData<Resource<List<SmartBusExpanableData>>>()
    private val mutableGroupPos = MutableLiveData<Int>()
    private val mutableTimeString = MutableLiveData<String>()
    private val mutableStartStationBean = MutableLiveData<String>()
    private val mutableEndStationBean = MutableLiveData<String>()

    private val mutableBusOrderBean = MutableLiveData<SmartBusRequestBean>()


    val busLiveData: LiveData<Resource<List<SmartBusExpanableData>>> = mutableBusData.switchMap {
        getLiveData { BusRepo.getSmartBusLineData() }
    }

    val choiceItemData: LiveData<SmartBusExpanableData> = mutableGroupPos.switchMap { pos ->
        liveData(Dispatchers.IO) {
            if (busLiveData.value is Resource.Success) {
                busLiveData.value?.data.apply {
                    emit(this!![pos])
                }
            }
        }
    }

    val timeString: LiveData<String> = mutableTimeString.map { it }

    val startStationData: LiveData<String> = mutableStartStationBean.switchMap {
        liveData {
            emit(it)
        }
    }
    val endStationData = mutableEndStationBean.switchMap {
        liveData {
            emit(it)
        }
    }

    val endStationList: LiveData<List<String>> = mutableStartStationBean.switchMap { str ->
        liveData(Dispatchers.IO) {
            choiceItemData.value!!.childData.also {
                val stepsId = it.findLast { data -> data.name == str }?.stepsId
                stepsId?.apply {
                    val list = it.filter { data ->
                        data.stepsId > stepsId
                    }.map { it.name }
                    emit(list)
                } ?: emit(it.map { it.name })

            }
        }
    }

    val busOrderResult: LiveData<Resource<BusOrderResponseBean>> = mutableBusOrderBean.switchMap {
        getLiveData { BusRepo.postBusOrder(it) }
    }

    val startStationList: LiveData<List<String>> = choiceItemData.map { data ->
        val mutableList = data.childData.toMutableList()
        mutableList.removeLast()
        mutableList.map { it.name }
    }

    fun getBusExpanableData() {
        mutableBusData.value = mutableBusData.value
    }

    fun getChoiceData(pos: Int) {
        mutableGroupPos.value = pos
    }

    fun putTimeString(timeString: String) {
        mutableTimeString.value = timeString
    }

    fun putStartStation(startString: String) {
        mutableStartStationBean.value = startString
    }

    fun putEndStation(endString: String) {
        mutableEndStationBean.value = endString
    }

    fun postBusOrder(order: SmartBusRequestBean) {
        mutableBusOrderBean.value = order
    }


}